package com.qf.kdxf.service.impl;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.qf.kdxf.entity.facerecognition.DataResponse;
import com.qf.kdxf.entity.facerecognition.ResponseData;
import com.qf.kdxf.service.FaceRecognitionService;
import com.qf.kdxf.util.FaceRecognitionUtil;
import com.qf.kdxf.util.common.FileUtil;
import com.qf.kdxf.util.common.HttpUtil;
import org.springframework.stereotype.Service;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author 明廷盛
 * @Date 2025/6/16 13:31
 * @Description
 */
@Service
public class FaceRecognitionServiceImpl implements FaceRecognitionService {
    public final static String requestUrl = "https://api.xf-yun.com/v1/private/s67c9c78c";
    public final static String appid = "ced63297"; //请填写控制台获取的APPID,
    public final static String apiSecret = "MDU2NjU1YzE5OTQ0MDk1NzliNjkzNzY5";//请填写控制台获取的APISecret;
    public final static String apiKey = "fa3ef253b402aedd87a92676047e5010";  //请填写控制台获取的APIKey
    public final static String serviceId = "s67c9c78c";
    public final static Gson json = new Gson();

    /**
     * 01-人脸对比
     *
     * @param imageFirstUrl
     * @param imageSecondUrl
     * @return
     */
    @Override
    public ResponseData faceContrast(String imageFirstUrl, String imageSecondUrl) {
        String url = FaceRecognitionUtil.assembleRequestUrl(requestUrl, apiKey, apiSecret);
        String imageBase641 = null;
        String imageBase642 = null;
        System.out.println(imageBase641);
        String imageEncoding1 = null;
        String imageEncoding2 = null;
        System.out.println(imageEncoding1);
        imageBase641 = Base64.getEncoder().encodeToString(FileUtil.read(imageFirstUrl));
        imageBase642 = Base64.getEncoder().encodeToString(FileUtil.read(imageSecondUrl));
        imageEncoding1 = imageFirstUrl.substring(imageFirstUrl.lastIndexOf(".") + 1);
        imageEncoding2 = imageSecondUrl.substring(imageSecondUrl.lastIndexOf(".") + 1);
        System.out.println("url:" + url);
        // TODO URL炸了,
//        url = "https://api.xf-yun.com/v1/private/s67c9c78c?authorization=YXBpX2tleT0iZmEzZWYyNTNiNDAyYWVkZDg3YTkyNjc2MDQ3ZTUwMTAiLCBhbGdvcml0aG09ImhtYWMtc2hhMjU2IiwgaGVhZGVycz0iaG9zdCBkYXRlIHJlcXVlc3QtbGluZSIsIHNpZ25hdHVyZT0idllsR1YrUUF0bTZvd2YzdGZZd0dYTFY2Z1hPcXYyekhKaGk3NXM2a1Vnbz0i&host=api.xf-yun.com%3A-1&date=Mon%2C+16+Jun+2025+08%3A52%3A16+GMT";
        return FaceRecognitionUtil.handleFaceContrastRes(url, FaceRecognitionUtil.getXParam01(imageBase641, imageEncoding1, imageBase642, imageEncoding2, appid, serviceId), json);
    }

    /**
     * 02-人脸检测
     *
     * @param imagePath
     * @return
     */
    @Override
    public ResponseData faceContrast(String imagePath) {
        String url = FaceRecognitionUtil.assembleRequestUrl(requestUrl, apiKey, apiSecret);

        String imageBase641 = Base64.getEncoder().encodeToString(FileUtil.read(imagePath));
        String imageEncoding1 = imagePath.substring(imagePath.lastIndexOf(".") + 1);
        //System.out.println("url:"+url);
        return FaceRecognitionUtil.handleFaceContrastRes(url, FaceRecognitionUtil.getXParam02(imageBase641, imageEncoding1, appid, serviceId), json);
    }


    /**
     * 03-人脸特征分析（颜值、年龄、表情、性别）
     */
    // 接口密钥(webapi类型应用开通人脸特征分析服务后，控制台--我的应用---人脸特征分析---服务的apikey)
    private static final String API_KEY = "e7b4d1e682c797f5de7ad0c61eb9a58f";

    @Override
    public HashMap<String, String> getFeatures(String filePath, String faceImage) {
        HashMap<String, String> results = new HashMap<>();
        Map<String, String> urls = new HashMap<>();
        urls.put("sex", "http://tupapi.xfyun.cn/v1/sex");
        urls.put("age", "http://tupapi.xfyun.cn/v1/age");
        urls.put("expression", "http://tupapi.xfyun.cn/v1/expression");
        urls.put("face_score", "http://tupapi.xfyun.cn/v1/face_score");
        byte[] imageByteArray = FileUtil.read(filePath);

        for (String key : urls.keySet()) {
            Map<String, String> header = FaceRecognitionUtil.buildHttpHeader(faceImage, API_KEY, appid);
            String result = HttpUtil.doPost1(urls.get(key), header, imageByteArray);
            DataResponse dataResponse = JSON.parseObject(result, DataResponse.class);
            int label = dataResponse.getData().getFileList().get(0).getLabel();
            String value = null;
            if (key.equals("sex")) {
                switch (label) {
                    case 0:
                        value = "男人";
                        break;
                    case 1:
                        value = "女人";
                        break;
                    case 2:
                        value = "难以辨认";
                        break;
                    case 3:
                        value = "多人";
                        break;
                }

            }
            if (key.equals("age")) {
                switch (label) {
                    case 0:
                        value = "0-1";
                        break;
                    case 1:
                        value = "2-5";
                        break;
                    case 2:
                        value = "6-10";
                        break;
                    case 3:
                        value = "11-15";
                        break;
                    case 4:
                        value = "16-20";
                        break;
                    case 5:
                        value = "21-25";
                        break;
                    case 6:
                        value = "31-40";
                        break;
                    case 7:
                        value = "41-50";
                        break;
                    case 8:
                        value = "51-60";
                        break;
                    case 9:
                        value = "61-80";
                        break;
                    case 10:
                        value = "80以上";
                        break;
                    case 11:
                        value = "其他";
                        break;
                    case 12:
                        value = "26-30";
                        break;
                }

            }
            if (key.equals("expression")) {
                switch (label) {
                    case 0:
                        value = "其它（非人脸表情图片）";
                        break;
                    case 1:
                        value = "其它表情";
                        break;
                    case 2:
                        value = "喜悦";
                        break;
                    case 3:
                        value = "愤怒";
                        break;
                    case 4:
                        value = "悲伤";
                        break;
                    case 5:
                        value = "惊恐";
                        break;
                    case 6:
                        value = "厌恶";
                        break;
                    case 7:
                        value = "中性";
                        break;
                }

            }
            if (key.equals("face_score")) {
                switch (label) {
                    case 0:
                        value = "漂亮";
                        break;
                    case 1:
                        value = "好看";
                        break;
                    case 2:
                        value = "普通";
                        break;
                    case 3:
                        value = "难看";
                        break;
                    case 4:
                        value = "其他";
                        break;
                    case 5:
                        value = "半人脸";
                        break;
                    case 6:
                        value = "多人";
                        break;
                }


            }
            System.out.println(value);
            results.put(key, value);
        }

        return results;
    }
}
